package com.cq.base.test;

import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.EntityPersister;

import com.cq.base.test.BaseTestCase;

/**
 * 使用BaseHibernateMappingTestCase, 测试sessionfactory中定义的orm mapping是否正确。
 */
public class BaseHibernateMappingTestCase extends BaseTestCase {

	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public void testColumnMapping() throws Exception {
		SessionFactory sf = sessionFactory;
		Session session = sf.openSession();
		try {
			Map metadata = sf.getAllClassMetadata();
			for (Object o : metadata.values()) {
				EntityPersister persister = (EntityPersister) o;
				String className = persister.getEntityName();
				log.debug("Trying select * from: " + className);
				Query q = session.createQuery("from " + className + " c");
				q.iterate();
				log.debug("ok: " + className);
			}
		} finally {
			session.close();
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
